<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-trace.html" />
<link rel="prev" href="module-hotshot.html" />
<link rel="parent" href="profile.html" />
<link rel="next" href="node807.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>25.9 timeit -- Measure execution time of small code snippets</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="25.8.3 example Usage"
  href="hotshot-example.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="25. the Python Profilers"
  href="profile.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="25.9.1 command Line Interface"
  href="node807.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="hotshot-example.html">25.8.3 Example Usage</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="profile.html">25. The Python Profilers</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node807.html">25.9.1 Command Line Interface</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION0027900000000000000000">
25.9 <tt class="module">timeit</tt> --
         Measure execution time of small code snippets</a>
</h1>

<p>
<a name="module-timeit"></a>

<p>

<span class="versionnote">New in version 2.3.</span>

<a id='l2h-5122' xml:id='l2h-5122'></a>

<p>
This module provides a simple way to time small bits of Python code.
It has both command line as well as callable interfaces.  It avoids a
number of common traps for measuring execution times.  See also Tim
Peters' introduction to the ``Algorithms'' chapter in the
<em class="citetitle"
 >Python Cookbook</em>, published by O'Reilly.

<p>
The module defines the following public class:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-5118' xml:id='l2h-5118' class="class">Timer</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>stmt=<code>'pass'</code>
                         </var><big>[</big><var>, setup=<code>'pass'</code>
                         </var><big>[</big><var>, timer=&lt;timer function&gt;</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Class for timing execution speed of small code snippets.

<p>
The constructor takes a statement to be timed, an additional statement
used for setup, and a timer function.  Both statements default to
<code>'pass'</code>; the timer function is platform-dependent (see the
module doc string).  The statements may contain newlines, as long as
they don't contain multi-line string literals.

<p>
To measure the execution time of the first statement, use the
<tt class="method">timeit()</tt> method.  The <tt class="method">repeat()</tt> method is a
convenience to call <tt class="method">timeit()</tt> multiple times and return a list
of results.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5119' xml:id='l2h-5119' class="method">print_exc</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>file=<tt class="constant">None</tt></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Helper to print a traceback from the timed code.

<p>
Typical use:

<p>
<div class="verbatim"><pre>
    t = Timer(...)       # outside the try/except
    try:
        t.timeit(...)    # or t.repeat(...)
    except:
        t.print_exc()
</pre></div>

<p>
The advantage over the standard traceback is that source lines in the
compiled template will be displayed.
The optional <var>file</var> argument directs where the traceback is sent;
it defaults to <code>sys.stderr</code>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5120' xml:id='l2h-5120' class="method">repeat</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>repeat<code>=3</code> </var><big>[</big><var>,
                           number<code>=1000000</code></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Call <tt class="method">timeit()</tt> a few times.

<p>
This is a convenience function that calls the <tt class="method">timeit()</tt>
repeatedly, returning a list of results.  The first argument specifies
how many times to call <tt class="method">timeit()</tt>.  The second argument
specifies the <var>number</var> argument for <tt class="function">timeit()</tt>.

<p>
<div class="note"><b class="label">Note:</b>
It's tempting to calculate mean and standard deviation from the result
vector and report these.  However, this is not very useful.  In a typical
case, the lowest value gives a lower bound for how fast your machine can run
the given code snippet; higher values in the result vector are typically not
caused by variability in Python's speed, but by other processes interfering
with your timing accuracy.  So the <tt class="function">min()</tt> of the result is
probably the only number you should be interested in.  After that, you
should look at the entire vector and apply common sense rather than
statistics.
</div>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5121' xml:id='l2h-5121' class="method">timeit</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>number<code>=1000000</code></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Time <var>number</var> executions of the main statement.
This executes the setup statement once, and then
returns the time it takes to execute the main statement a number of
times, measured in seconds as a float.  The argument is the number of
times through the loop, defaulting to one million.  The main
statement, the setup statement and the timer function to be used are
passed to the constructor.

<p>
<div class="note"><b class="label">Note:</b>
By default, <tt class="method">timeit()</tt> temporarily turns off garbage collection
during the timing.  The advantage of this approach is that it makes
independent timings more comparable.  This disadvantage is that GC
may be an important component of the performance of the function being
measured.  If so, GC can be re-enabled as the first statement in the
<var>setup</var> string.  For example:
<div class="verbatim"><pre>
    timeit.Timer('for i in xrange(10): oct(i)', 'gc.enable()').timeit()
</pre></div>
</div>
</dl>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="node807.html">25.9.1 Command Line Interface</a>
<li><a href="node808.html">25.9.2 Examples</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="25.8.3 example Usage"
  href="hotshot-example.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="25. the Python Profilers"
  href="profile.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="25.9.1 command Line Interface"
  href="node807.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="hotshot-example.html">25.8.3 Example Usage</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="profile.html">25. The Python Profilers</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node807.html">25.9.1 Command Line Interface</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
